/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    vector<int> preorderTraversal(TreeNode* root) {
        vector<int> ret;
        if (!root) {
            return ret;
        }

        deque<TreeNode *> stack;
        stack.push_back(root);

        while(stack.size() > 0) {
            TreeNode *node = stack.back();
            stack.pop_back();
            while(node) {
                ret.push_back(node->val);
                if (node->right) 
                    stack.push_back(node->right);
                node = node->left;
            }
        }
        return ret;
        
    }
};
